package com.jinzhi.wxpay.controller;



import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.wxpay.entity.OrderInfo;
import com.jinzhi.wxpay.entity.OrderInfoVO;
import com.jinzhi.wxpay.entity.OrderParme;
import com.jinzhi.wxpay.enums.OrderStatus;
import com.jinzhi.wxpay.service.OrderInfoService;
import com.jinzhi.wxpay.vo.R;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@Api(tags = "商品订单管理")
@Controller
@RequestMapping("/api/orderInfo")
public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;



    /**
     * 充值详情页
     * */
    @GetMapping("/info")
    String JazUserIntegral(){
        return "templates/jzweb/jazUserAunt/add.html";
    }


    /**
     *查询出充值列表，并根据订单创建时间进行倒序排序
     * */
    @PostMapping  ("/list")
    @ResponseBody
    public Result<?> list(String current, String number,OrderParme  orderParme){//current当前页码，number每页显示条数//Date beginTime,Date endTime
        Page<OrderParme> page = orderInfoService.listOrderByCreateTimeDesc(Integer.valueOf(current), Integer.valueOf(number),orderParme);
        return  Result.ok(page);
    }


    /**
     * 查询出详情信息
     * */
    @PostMapping("/getOrder")
    @ResponseBody
    public Result<OrderInfo> getOrder(Long id){

        return  Result.ok(orderInfoService.selectById(id));
    }

    /**
     * 更改充值备注
     * */
    @PostMapping("/edit")
    @ResponseBody
    public  Result<String>  edit(OrderInfo  orderInfo){
        EntityWrapper<OrderInfo> Wrapper = new EntityWrapper<>();
        Wrapper.eq("id",orderInfo.getId());
        orderInfoService.update(orderInfo,Wrapper);
        return Result.ok();
    }



//

    /**
     * 根据订单id查询本地订单状态(当二维码打开后前端就开启定时任务，调用这个接口，查看支付是否已经成功)
     * */
    @PostMapping("/query-order-status/{orderNo}")
    public  R  queryOrderStatus(@PathVariable String orderNO){
        OrderInfo orderStatus = orderInfoService.getOrderStatus(orderNO);
        if(OrderStatus.SUCCESS.getType().equals(orderStatus.getOrderStatus())){//枚举判断
            return R.ok().setMessage("支付成功");//支付成功
        }
        return  R.ok().setCode(101).setMessage("支付中");//返回101就是支付中
    }



    /**
     * 充值信息导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, OrderParme  orderParme) {
        List<OrderInfoVO> list1 = new LinkedList<>();
        List<OrderInfo> list=	orderInfoService.OrderInfoExcel(orderParme);
        for(OrderInfo li:list){
            OrderInfoVO orderInfoVO=new OrderInfoVO();
            orderInfoVO.setOrderRemark(li.getOrderRemark());
            orderInfoVO.setTotalFee(li.getTotalFee());
            if (li.getCreateTime() != null ){
                DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String  dateStr = sdf.format(li.getCreateTime());
                orderInfoVO.setCreateTime(dateStr);
            }
            String userName = "";
            if (li.getName() != null&&!li.getName().equals("")){
                userName = li.getName();
            }else {
                userName = li.getUsername();
            }
            orderInfoVO.setUsername(userName);
            list1.add(orderInfoVO);
        }
        //导出操作
        EasyPoiUtil.exportExcel(list1, "充值信息导出", "充值信息导出", OrderInfoVO.class, "充值信息导出", response);
    }




}
